/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include <stdio.h>
#include "tim.h"
#include "hardware_config.h"
#include "led.h"
#include "button.h"
//#include "button.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

led_t boardLed;
led_t userLed;
//button_t leftBtn;
button_t btn;

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
        .name = "defaultTask",
        .stack_size = 128 * 4,
        .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for ledTask */
osThreadId_t ledTaskHandle;
const osThreadAttr_t ledTask_attributes = {
        .name = "ledTask",
        .stack_size = 300 * 4,
        .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for buttonTask */
osThreadId_t buttonTaskHandle;
const osThreadAttr_t buttonTask_attributes = {
        .name = "buttonTask",
        .stack_size = 300 * 4,
        .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for usart1Mutex */
osMutexId_t usart1MutexHandle;
const osMutexAttr_t usart1Mutex_attributes = {
        .name = "usart1Mutex"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);

void start_led_task(void *argument);

void start_button_task(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void)
{
    /* USER CODE BEGIN Init */
    int ret = 0;

    led_create(&boardLed, 0, board_led_set_level);
    led_create(&userLed, 0, user_led_set_level);

//    button_create(&leftBtn, left_btn_pressed, btn_get_tick);

    HAL_TIM_Base_Start_IT(&htim4);

    button_create(&btn, 0, get_btn_level);
    button_start(&btn);

    /* USER CODE END Init */
    /* Create the mutex(es) */
    /* creation of usart1Mutex */
    usart1MutexHandle = osMutexNew(&usart1Mutex_attributes);

    /* USER CODE BEGIN RTOS_MUTEX */
    /* add mutexes, ... */
    /* USER CODE END RTOS_MUTEX */

    /* USER CODE BEGIN RTOS_SEMAPHORES */
    /* add semaphores, ... */
    /* USER CODE END RTOS_SEMAPHORES */

    /* USER CODE BEGIN RTOS_TIMERS */
    /* start timers, add new ones, ... */
    /* USER CODE END RTOS_TIMERS */

    /* USER CODE BEGIN RTOS_QUEUES */
    /* add queues, ... */
    /* USER CODE END RTOS_QUEUES */

    /* Create the thread(s) */
    /* creation of defaultTask */
    defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

    /* creation of ledTask */
    ledTaskHandle = osThreadNew(start_led_task, NULL, &ledTask_attributes);

    /* creation of buttonTask */
    buttonTaskHandle = osThreadNew(start_button_task, NULL, &buttonTask_attributes);

    /* USER CODE BEGIN RTOS_THREADS */
    /* add threads, ... */
    /* USER CODE END RTOS_THREADS */

    /* USER CODE BEGIN RTOS_EVENTS */
    /* add events, ... */
    /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
    /* USER CODE BEGIN StartDefaultTask */

    /* Infinite loop */
    for (;;)
    {
        osDelay(1);
    }
    /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_start_led_task */
/**
* @brief Function implementing the ledTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_start_led_task */
void start_led_task(void *argument)
{
    /* USER CODE BEGIN start_led_task */
    static uint32_t lastTick;
    lastTick = HAL_GetTick();

    /* Infinite loop */
    for (;;)
    {
        led_open(&boardLed);
        led_close(&userLed);
        HAL_Delay(1000);

        led_close(&boardLed);
        led_open(&userLed);
        HAL_Delay(1000);

        osDelay(1);
    }
    /* USER CODE END start_led_task */
}

/* USER CODE BEGIN Header_start_button_task */
/**
* @brief Function implementing the buttonTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_start_button_task */
void start_button_task(void *argument)
{
    /* USER CODE BEGIN start_button_task */
    static uint8_t event;

    /* Infinite loop */
    for (;;)
    {
        if (event != get_button_event(&btn))
        {
            event = get_button_event(&btn);

            if (event == ButtonEvent_SingleClick)
            {
                //do something
                printf("SINGLE_CLICK\n");
            }
            else if (event == ButtonEvent_DoubleClick)
            {
                //do something
                printf("DOUBLE_CLICK\n");
            }
            else if (event == ButtonEvent_LongPressStart)
            {
                //do something
                printf("LONG_PRESS_START\n");
            }
            else if (event == ButtonEvent_LongPressHold)
            {
                //do something
                printf("LONG_PRESS_HOLD\n");
            }
            else
            {
            }
        }

        osDelay(1);
    }
    /* USER CODE END start_button_task */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */


/**
  * @brief  周期5ms的定时器中断
  * @param  htim TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    button_ticks();
}


/* USER CODE END Application */

